//
//  CartItemTableViewCell.swift
//  StoryDemo
//
//  Created by mac on 2019/7/12.
//  Copyright © 2019年 zhkj. All rights reserved.
//

import UIKit
import SwiftyJSON

struct CartNum {
    var Success: String?
    var Url: String?
    
    init(json: JSON) {
        Success = json["Success"].stringValue
        Url = json["Url"].stringValue
    }
}

class CartItemTableViewCell: UITableViewCell {
    var shopcartCellBlock:((_ isSelected:Bool) -> Void)?
    var shopcartCellEditBlock:((_ count:Int) -> Void)?
    var shopcartCountViewEditBlock:((_ count:Int) -> Void)?
    
    var model : CartBean! {
        didSet{
            if ((model.ImgUrl) != ""){
                icon.setImage(path: URL.init(string: (model.ImgUrl)!)!)
            }
            name.text=model.Name
            var str=""
            if model.Size != ""&&model.Version != ""&&model.Color != "" {
                str.append("\(model.Size!);\(model.Version!);\(model.Color!)")
            }
            if model.Size == ""&&model.Version != ""&&model.Color != "" {
                str.append("\(model.Version!);\(model.Color!)")
            }
            if model.Size != ""&&model.Version == ""&&model.Color != "" {
                str.append("\(model.Size!);\(model.Color!)")
            }
            if model.Size != ""&&model.Version != ""&&model.Color == "" {
                str.append("\(model.Size!);\(model.Version!)")
            }
            if model.Size == ""&&model.Version == ""&&model.Color != "" {
                str.append("\(model.Color!)")
            }
            if model.Size != ""&&model.Version == ""&&model.Color == "" {
                str.append("\(model.Size!)")
            }
            if model.Size == ""&&model.Version != ""&&model.Color == "" {
                str.append("\(model.Version!)")
            }
            label.text=str
            price.text="￥\(model.Price!)"
            price.textColor=UIColor.red
            num_btn.currentNumber=String(model.Count)
            //设置边框颜色
            num_btn.borderColor(UIColor.gray)
            //加减按钮的闭包回调
            num_btn.numberResult { (number) in
                
                self.count=Int(number)!
                /**
                 * skus : [{"skuId":"232_90_0_101","count":"1"}]
                 * userkey : RnlhdTc0bC9LL2ZqN28zc2FsOFMvQT09
                 */
                let json="{\"skus\":[{\"skuId\":\"\(self.model.SkuId!)\",\"count\":\"\(self.count!)\"}],\"userkey\":\"\(userkey!)\"}"
                self.changeCount(jsonstr: json)
                print(number)
                print(json)
            }
            btn_check.isSelected=model.isSelected
        }
    }
    
    @IBOutlet weak var uv_check: UIView!
    @IBOutlet weak var btn_check: UIButton!
    @IBOutlet weak var icon: UIImageView!
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var price: UILabel!
    @IBOutlet weak var num_btn: PPNumberButton!
    var count:Int!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        btn_check.setImage(UIImage(named:"circle"), for: .normal)
        btn_check.setImage(UIImage(named:"yuangou"), for: .selected)
        btn_check.addOnClickListener(target: self, action: #selector(check))
        uv_check.addOnClickListener(target: self, action: #selector(check))
        shopcartCountViewEditBlock = {(count) in
            if self.shopcartCellEditBlock != nil{
                self.shopcartCellEditBlock!(count)
            }
        }
    }
    @objc func check(){
        btn_check.isSelected = !btn_check.isSelected
        if shopcartCellBlock != nil{
            shopcartCellBlock!(btn_check.isSelected)
        }
    }
    func changeCount(jsonstr:String){
        var d = ["userkey":userkey,
                 "jsonstr":jsonstr,
                 "app_key":app_key,
                 "timestamp":getTimestamp()
            ] as! [String : String]
        let sign=SignTopRequest(params: d)
        d["sign"]=sign
        AlamofireHelper.post(url: PostUpdateCartItem, parameters: d, successHandler: {[weak self](res)in
            HUD.dismiss()
            guard let ss = self else {return}
            if CartNum.init(json: res).Success=="true"{
                if ss.shopcartCountViewEditBlock != nil{
                    ss.shopcartCountViewEditBlock!(ss.count)
                }
                ss.model.Count=ss.count
            }
        }){[weak self] (error) in
            HUD.dismiss()
            guard let ss = self else {return}
        }
    }
//    override var frame: CGRect {
//        get {
//            return super.frame
//        }
//        set {
//            var frame = newValue
//            frame.origin.x += 10
//            frame.size.width -= 2 * 10
//            super.frame = frame
//        }
//    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
